#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash samtools-1.9.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* ||  "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* ||  "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar environment-modules

wget -t 0 -c -P $buildpath https://github.com/samtools/samtools/releases/download/1.9/samtools-1.9.tar.bz2
tar -xf $buildpath/samtools-1.9.tar.bz2 -C $buildpath && rm -rf $buildpath/samtools-1.9.tar.bz2
cd $buildpath/samtools-1.9
CC=clang ./configure --without-curses --prefix=$installpath/samtools-1.9
make -j$(nproc) && make install
cat>"$installpath/samtools-1.9/samtools_modulefiles"<<EOF
#%Module1.0
conflict samtools
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set samtools \$pwd
setenv samtools \$samtools
prepend-path PATH \$samtools/bin
EOF

rm -rf $buildpath/samtools-1.9

